/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actors;

import graphics.Images;
import graphics.RenderInstruction;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import resources.images.ResourceStub;

/**
 *
 * @author Joshua
 */
public class BlockadeActor extends Actor {

    int length, width;

    public BlockadeActor(int x1, int y1, int length, int width) {
        this.x = x1;
        this.length = length;
        this.y = y1;
        this.width = width;
        ActorManager.addBoundingShape(this.collidableArea()[0], false);
    }

    public BlockadeActor(int x1, int y1, int width) {
        this(x1, y1, width, width);
    }

    @Override
    public void act() {
        //no acting
    }

    @Override
    public RenderInstruction render() {
        //no rendering
        //return new NullRenderInstruction();
        try {
            return new BlockadeActorRender(this);
        } catch (IOException e) {
            System.err.println("Couldn't load image resource");
            System.exit(1);
            return null;
        }
    }

    @Override
    public void collide(Actor collidedWith) {
        return;
    }

    @Override
    public Shape[] collidableArea() {
        Shape[] returnArray;
        returnArray = new Shape[1];
        returnArray[0] = new Rectangle((int)x, (int)y, length, width);
        return returnArray;
    }
}

class BlockadeActorRender extends RenderInstruction {

    private static boolean display = true;
    private BlockadeActor actor;
    private static VolatileImage blockadeImage;
    private static VolatileImage blockadeSheet;
    private VolatileImage actualRender;

    public BlockadeActorRender(BlockadeActor a) throws IOException {
        actor = a;
        if (blockadeImage == null) {
            blockadeImage = Images.createVolatileImage(120, 120, Transparency.BITMASK);
            redrawBase();
        }
        if (blockadeSheet == null) {
            blockadeSheet = Images.createVolatileImage(512 + 2 * (blockadeImage.getHeight()), 512 + 2 * (blockadeImage.getWidth()), Transparency.BITMASK);
            redrawComposite();
        }
        actualRender = Images.createVolatileImage(actor.length, actor.width, Transparency.BITMASK);
        Graphics2D g2 = (Graphics2D) actualRender.getGraphics();
        g2.drawImage(blockadeSheet, blockadeImage.getWidth() / -2, blockadeImage.getHeight() / -2, null);
        g2.dispose();
    }

    private static void redrawBase() throws IOException {
        BufferedImage temp = ImageIO.read(ResourceStub.class.getResource("blockade.jpg"));
        Graphics2D g = (Graphics2D) blockadeImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        // These commands cause the Graphics2D object to clear to (0,0,0,0).
        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, blockadeImage.getWidth(), blockadeImage.getHeight()); // Clears the image.
        g.drawImage(temp, 0, 0, null);
        g.dispose();
    }

    private void redrawComposite() {
        Graphics2D g = (Graphics2D) blockadeSheet.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, blockadeImage.getWidth(), blockadeImage.getHeight()); // Clears the image.
        for (int i = 0; i < (blockadeSheet.getWidth() / blockadeImage.getWidth()); i++) {
            for (int j = 0; j < (blockadeSheet.getHeight() / blockadeImage.getHeight()); j++) {
                g.drawImage(blockadeImage, i * blockadeImage.getWidth(), j * blockadeImage.getHeight(), null);
            }

        }
    }

    @Override
    public void render(Graphics2D g) {
        g.setColor(new Color(0, 0, 0, 0));
        try {
            if (blockadeImage.contentsLost()) {
                redrawBase();
            }
            if (blockadeSheet.contentsLost()) {
                redrawComposite();
            }
            if (actualRender.contentsLost()) {
                Graphics2D g2 = (Graphics2D) actualRender.getGraphics();
                g2.drawImage(blockadeSheet, blockadeImage.getWidth() / -4, blockadeImage.getHeight() / -4, null);
                g2.dispose();
            }
        } catch (IOException e) {
            System.err.println("Could not load image files");
            System.exit(1);
        }
        g.drawImage(actualRender,(int) actor.x,(int) actor.y, null);
        /*g.setColor(new Color(0, 0, 0, 0));

        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f));
        g.drawRect(6, 6, blockadeImage.getWidth()-6, blockadeImage.getHeight()-6);
        g.drawRect(5, 5, blockadeImage.getWidth()-5, blockadeImage.getHeight()-5);
        
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f));

        g.drawRect(4, 4, blockadeImage.getWidth()-4, blockadeImage.getHeight()-4);
        g.drawRect(3, 3, blockadeImage.getWidth()-3, blockadeImage.getHeight()-3);

        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .8f));
        g.drawRect(2, 2, blockadeImage.getWidth()-2, blockadeImage.getHeight()-2);
        
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        g.drawRect(0, 0, blockadeImage.getWidth(), blockadeImage.getHeight());*/
    }
}
